package com.atguigu.gulimall.order.web.controller;

import com.atguigu.common.exception.NoStockException;
import com.atguigu.gulimall.order.service.OmsOrderService;
import com.atguigu.gulimall.order.vo.OderSubmitVo;
import com.atguigu.gulimall.order.vo.OrderConfirmVo;
import com.atguigu.gulimall.order.vo.SubmitOrderResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ExecutionException;

@Controller
public class OrderController {

    @Autowired
    OmsOrderService omsOrderService;
    @GetMapping("/{path}.html")
    public String test(@PathVariable("path")String path){
        return path;
    }


    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo orderConfirmVo=omsOrderService.toTrade();
        model.addAttribute("orderConfirm",orderConfirmVo);
        return "account";
    }

    @PostMapping("/submitOrder")
    public String submitOrder(Model model, OderSubmitVo oderSubmitVo, RedirectAttributes redirectAttributes){
         try {
             SubmitOrderResponseVo submitOrderResponseVo=omsOrderService.submitOrder(oderSubmitVo);
             if(submitOrderResponseVo.getCode()==0){
                 model.addAttribute("oderSubmitVo",oderSubmitVo);
                 return "pay";
             }else{
                 return "redirect:http://order.gulimall.com/toTrade";
             }
         }catch (ArithmeticException o){
             return "redirect:http://order.gulimall.com/toTrade";
         }catch (Exception e){
             return "redirect:http://order.gulimall.com/toTrade";
         }

    }
}
